<template>
  <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">若依后台管理系统</h3>
      <el-form-item prop="username">
        <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="姓名">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="gender">
        <el-form-item prop="gender">
          <template>
            <el-radio v-model="registerForm.gender" label="男">男</el-radio>
            <el-radio v-model="registerForm.gender" label="女">女</el-radio>
          </template>
        </el-form-item>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input v-model="registerForm.phone" type="text" auto-complete="off" placeholder="电话">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <!-- 图像验证码 -->
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input v-model="registerForm.code" type="text" name="" style="width: 180px;" id="imageCode" placeholder="请输入图片验证码">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <img id="register-code" @click="getImage"
               :src="imageCode"

               class="captcha-image-temp" alt="点击换图" title="点击换图"
               style="vertical-align: middle; cursor: pointer;">
        </div>
      </el-form-item>
      <el-form-item prop="wx">
        <el-input v-model="registerForm.wx" type="text" auto-complete="off" placeholder="微信">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item label="小区" prop="villageId">
        <template>
          <el-select v-model="registerForm.villageId" placeholder="请选择小区">
            <el-option
              v-for="item in villages"
              :key="item.id"
              :label="item.villageName"
              :value="item.id">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="楼栋" prop="buildingId">
        <template>
          <el-select v-model="registerForm.buildingId" placeholder="请选择楼栋">
            <el-option
              v-for="item in buildings"
              :key="item.id"
              :label="item.buildingName"
              :value="item.id">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item prop="bindingAddress">
        <el-input v-model="registerForm.buildingAddress" type="text" auto-complete="off" placeholder="住址">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import {listVillage} from "@/api/basicdata/village";
import {listBuilding} from "@/api/basicdata/building";
import {getImgCode,registerNew} from "@/api/basicdata/user";

export default {
  name: "Register",
  data() {
    return {
      queryParams4: {
        pageNum: 1,
        pageSize: 100,
        villageId: null,
        buildingId: null,
        resident: null,
        adminPhone: null,
        bulidingAddress: null,
        state: null,
        des: null
      },
      buildings:[],
      villages:[],
      imageCode:"",
      imageCodePrefix: "data:image/jpeg;base64,",
      registerForm: {
        username: "",
        gender:"",
        phone:"",
        code: "",
        uuid: "",
        wx:"",
        villageId:"",
        buildingId:"",
        buildingAddress:""
      },
      registerRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入姓名"},
          {min: 2, max: 20, message: '快点！', trigger: 'blur'}
        ],
        code: [{required: true, trigger: "change", message: "请输入验证码"}]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    this.getBuilding();
    this.getVillage();
    this.getImage();
  },
  methods: {
    // 获取图形验证码
    getImage(){
      // 2.1.从localStorage中获取UUID
      let imageCodeKey = localStorage.getItem("registerImageCodeKey");
      // 2.2.判断当前浏览器本地是否有key值，没有则生成，有则直接使用
      if (!imageCodeKey || imageCodeKey === '') {
        imageCodeKey = this.createUuid();
        localStorage.setItem("registerImageCodeKey", imageCodeKey);
      }
      // 2.3.发送Axios请求，传递UUID，得到图形验证码的Base64字符串
      getImgCode(imageCodeKey).then(res => {
        // 2.3.1.将接口返回的图形验证码Base64字符串码+前缀赋值给模型中的imageCode字段
        this.imageCode = this.imageCodePrefix + res.msg;
      })
    },

    // 查询所有楼栋
    getBuilding() {
      listBuilding(this.queryParams4).then(response => {
        this.buildings = response.rows;
      });
    },

    // 查询所有小区
    getVillage() {
      listVillage(this.queryParams4).then(response => {
        this.villages = response.rows;
      });
    },

    // 生成UUID方法
    createUuid() {
      var s = [];
      var hexDigits = "0123456789abcdefghi";
      for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
      }
      s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = "-";

      var uuid = s.join("");
      return uuid;
    },

    // 注册
    handleRegister() {
      this.registerForm.uuid = localStorage.getItem("registerImageCodeKey");
      this.loading=true;
      registerNew(this.registerForm).then(res=>{
        console.log(res)
        this.loading=false;
        let result = res.code
        if (result ===200){
          alert("注册成功")
        }
      })

    },
    // 监听器
    getBuildingByVillageId(id) {
      listBuilding(id).then(res=>{
        this.queryParams4.villageId = id
        this.getBuilding()
      })
    }
  },
  watch:{
    "registerForm.villageId":{
      immediate:true,
      handler(value) {
        if (!!value) {
          let id = this.registerForm.villageId
          this.getBuildingByVillageId(id)

        }
      }
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.register-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.register-code-img {
  height: 38px;
}
</style>
